package jianxinjinke;

import java.util.*;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2024/11/10-下午7:32
 * Since  :   1.0
 * Version:   1.0
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param arr int整型一维数组
     * @return int整型一维数组
     */
//3 4 1  2
    public static void main(String[] args) {
        int nums[] = {1,2, 2, 2,1,0};

        System.out.println(solve2(nums));
    }

    public static long solve2(int[] arr) {

        if (arr.length == 1) {
            return -1;
        }
        if (arr.length == 2) {
            if (arr[0] != arr[1]) {
                return -1;

            }
            return 0;
        }
        if (arr.length % 2 == 0) {
            int left = 0;
            int right = arr.length - 1;
            boolean flag = false;
            int count = 0;

            while (left < right) {

                int diff1 =  Math.abs(arr[right] - arr[left]);
                int diff2 =  Math.abs(arr[right - 1] - arr[left + 1]);
                if (diff1 != diff2) {
                    flag = true;
                    break;

                } else {
                    count += diff1;
                }

                left += 2;
                right -= 2;
            }
            if (flag) {
                return -1;
            } else {
                return count;
            }


        }

        int mid = arr.length / 2;
        int count = 0;
        int left = 0;
        int right = arr.length - 1;
        while (left < right-1) {
            int numLeft = arr[left];
            int numRight = arr[right];
            if (numLeft > numRight) {
                count += numLeft - numRight;
                arr[numRight - 1] += numLeft - numRight;
            } else if (numLeft < numRight) {
                count += numRight - numLeft;
                arr[left + 1] += numRight - numLeft;
            }
            left++;
            right--;
        }
        return count;


    }

    public static int[] solve(int[] arr) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i : arr) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }
        Set<Integer> integers = map.keySet();
        ArrayList<Integer> list = new ArrayList<>();
        List<List<Integer>> results = new ArrayList<>();
        for (int i : integers) {
            int count = map.get(i);
            if (count == 2) {
                list.add(i);
            }
        }

        for (int i : list) {
            ArrayList<Integer> list1 = new ArrayList<>();
            for (int j = 0; j < arr.length; j++) {
                if (arr[j] == i) {
                    list1.add(j);
                }
            }
            results.add(list1);

        }

        int[] myResults = new int[arr.length];
        Arrays.fill(myResults, -1);

        for (List<Integer> temp : results) {
            int index1 = temp.get(0);
            int index2 = temp.get(1);
            myResults[index1] = index2 + 1;
            myResults[index2] = index1 + 1;
        }
        return myResults;


    }


}
